﻿// ActionScript file
package application.view.components.UI{
	import caurina.transitions.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class ScrollContainer extends Sprite{
		protected var upArrow:Sprite;
		protected var downArrow:Sprite;
		protected var leftArrow:Sprite;
		protected var rightArrow:Sprite;
		protected var maskBitmap:Bitmap;
		protected var maskBitmapData:BitmapData;
		private var scrollDirection:String;
		private var changed:Boolean=false;
		//skin类 上下左右箭头、背景轨迹和拖拽块
		protected var _scrollBarArrow:Object;
		protected var _scrollBarThumb:Sprite;
		protected var _scrollBarTrack:Sprite;
		//滚动矩形
		private var _scrollRect:Rectangle;
		//是否显示scrollBar，有三个值:auto,off,on.
		private var _itemContainer:Sprite;
		protected var verticalScrollPolicy:String="auto";
		protected var horizontalScrollPolicy:String="auto";
		//滚动速率
		protected var _scrollSpeed:Number;
		private var _scrollH:Number;
		private var _scrollV:Number;
/* 		----------------------------------
		public properties
		-------------------------------- */
		//大多数情况下，scrollRect和实例的宽高是一致的，
		//scrollRect缩小，对container设置滤镜或其他，通过对container中的子项点击-->传递参数-->
		//ScrollContainer.active()滚动container;实现类似滚动效果。
		//
		public function set scrollBarArrow(value:Object):void{
			_scrollBarArrow=value;
		}
		public function get scrollBarArrow():Object{
			return _scrollBarArrow;
		}
		public function set scrollRect(value:Rectangle):void{
			_scrollRect=value;
		}
		public function set scrollH(value:Number):void{
			_scrollH=value;
		}
		public function set scrollV(value:Number):void{
			_scrollV=value;
		}
		public function setSize(w:Number,h:Number):void{
			this.width=w;
			this.heighth;
		}
		public function set itemContainer(value:Sprite):void{
			_itemContainer=value;
			buildContainer();
		}
/* 		---------------------------------
		constructor
	----------------------------------- */	
		public function ScrollContainer(itemContainer:Sprite=null){
			_itemContainer=itemContainer;
			init();
		}
		private function init():void {
			buildMaskShape();
			buildContainer();
			if (verticalScrollPolicy == "on" || verticalScrollPolicy == "auto") {
				buildVScrollBar();
			}else return;
			if (horizontalScrollPolicy == "on" || horizontalscrollpolicy == "auto"){
				buildHScrollBar();
			}else return;
		}
		//创建遮罩
		private function buildMaskShape():void{
			maskBitmapData=new BitmapData();
			maskBitmap=new Bitmap(maskBitmapData);
			addChild(maskBitmap);
			//maskBitmap.x&y =0;
			maskBitmapData.fillRect(_scrollRect,0x000000);
		}
		private function buildContainer():void{
			if(container == null){
			var container:Sprite=new _itemContainer;
			addChild(container);
			container.mask=maskBitmapData;
			changed=true;
		}
		//创建滚动条
		private function buildVScrollBar():void {
			//判断竖滚动条时，依据总高度(totalY)
			if (container.height > this.height) {	
				addVScrollBar();
			}
			if(container.width > this.width){
				addHScrollBar();
			}
		}
		private function addVScrollBar():void {
			upArrow=new _scrollBarArrow();
			addChild(upArrow);
			upArrow.addEventListener(MouseEvent.MOUSE_DOWN,onUpMouseDown);
			
			downArrow=new _scrollBarArrow()  ;
			addChild(downArrow);
			downArrow.rotation=180;
			downArrow.addEventListener(MouseEvent.MOUSE_DOWN,onDownMouseDown);
			measureVScrollBar();
			updateVScrollBar();
		}
		private function addHScrollBar():void{
			leftArrow=new _scrollBarArrow();
			addChild(leftArrow);
			leftArrow.rotation=90;
			leftArrow.addEventListener(MouseEvent.MOUSE_DOWN,onLeftMouseDown);
			
			rightArrow=new _scrollBarArrow()  ;
			addChild(rightArrow);
			rightArrow.rotation=270;
			rightArrow.addEventListener(MouseEvent.MOUSE_DOWN,onRightMouseDown);
			measureHScrollBar();
			updateHScrollBar();
		}
		//重绘滚动栏。根据scrollH/scrollV来更新滚动栏
		private function updateVScrollBar():void {
			if (container.y == 0) {
				//可以添加设置其消失，并添加消失效果
				upArrow.enabled=false;
				upArrow.mouseEnabled=false;
			}else {
				upArrow.enabled=true;
				upArrow.mouseEnabled=true;
			}
			if (container.y == container.height- this.height) {
				downArrow.enabled=false;
				downArrow.mouseEnabled=false;
			}else {
				downArrow.enabled=true;
				downArrow.mouseEnabled=true;
			}
			if(changed==true){
				updateScrollBarThumb();
			}
		}
		private function updateHScrollBar():void{
			if (container.x == 0) {
				//可以添加设置其消失，并添加消失效果
				leftArrow.enabled=false;
				leftArrow.mouseEnabled=false;
			}else {
				leftArrow.enabled=true;
				leftArrow.mouseEnabled=true;
			}
			if (container.x == container.width - this.width) {
				rightArrow.enabled=false;
				rightArrow.mouseEnabled=false;
			}else {
				rightArrow.enabled=true;
				rightArrow.mouseEnabled=true;
			}
			if(changed==true){
				updateScrollBarThumb();
			}
		}
		//VSrollBar的定位
		protected function measureVScrollBar():void{
		
		}
		protected function measureHScrollBar():void{
			
		}
		//更新/重绘滑块
		protected function updateScrollBarThumb():void{
		}
		
		//根据scrollH/scrollV来移动容器。
		private function activeItems(selectedIndex:uint):void {
			if (changed == true) {
				//无需考虑固定速率问题
				if(scrollDirection== "right"){
					Tweener.addTween(itemContainer,{_x:itemContainer.x-_scrollH,time:_scrollH/_scrollSpeed,transition:"easeOutExpo",onUpdate:tweenstop});
				}
				if(scrollDirection == "left"){
					Tweener.addTween(itemContainer,{_x:itemContainer.x+_scrollH,time:_scrollH/_scrollSpeed,transition:"easeOutExpo"});
				}
				if(scrollDirection == "up"){
					Tweener.addTween(itemContainer,{_y:itemContainer.y+_scrollV,time:_scrollH/_scrollSpeed,transition:"easeOutExpo"});
				}
				if(scrollDirection == "down"){
					Tweener.addTween(itemContainer,{_y:itemContainer.y-_scrollV,time:_scrollH/_scrollSpeed,transition:"easeOutExpo"});
				}
			}
			updateVScrollBar();
		}
		private function tweenStop():void{
			if(itemContainer.x>this.x || itemContainer.x < this.width-itemContainer.width){
				Tweener.removeTweens(itemContainer,"_x");
			}
			if(itemContainer.y>this.y || itemContainer.y < this.height-itemContainer.height){
				Tweener.removeTweens(itemContainer,"_y");
			}
		}
		/**-----------------------------------------
		Event Handlers
		-------------------------------------------*/
		private function onUpMouseDown(e:MouseEvent):void {
			scrollDirection="up";
			changed=true;
			activeitem();
		}
		private function onDownMouseDown(e:MouseEvent):void {
			scrollDirection="down";
			changed=true;
			activeitem();
		}
		private function onLeftMouseDown(e:MouseEvent):void {
			scrollDirection="left";
			changed=true;
			activeitem();
		}
		private function onRightMouseDown(e:MouseEvent):void {
			scrollDirection="right";
			changed=true;
			activeitem();
		}
	}
}